package edu.auctionmart

import org.joda.time.DateTime
import edu.auctionmart.Feedback;
import edu.auctionmart.User;
import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Feedback)
class FeedbackTests {

    void testValidation() {
       User userAccount1 = new User(username: "TestingFeedback", password: "Testing23Feedback")
	   User userAccount2 = new User(username: "TestingFeedback2", password: " Testing23Feedback")
	   
	   Item item = new Item(name: "Movie", description: "Some movie", category: ItemCategory.Electronics, quantity: 1)
	   
	   DateTime startTime = new DateTime(2012, 2, 12, 0, 0, 0)
	   DateTime endTime = new DateTime(2012, 2, 15, 0, 0, 0)
	   
	   Auction auction = new Auction(auctionTitle: "Title", startBid: 0.01, seller: userAccount1, item: item, startTime: startTime, endTime: endTime)
	   
	   Feedback feedback = new Feedback(leftBy: userAccount1, leftFor: userAccount2, auction: auction)
	   feedback.validate()
	   assert(feedback.hasErrors() == false)
	   
	   //no left for, should throw error
	   feedback = new Feedback(leftBy: userAccount1)
	   feedback.validate()
	   assert(feedback.hasErrors() == true)
	   
	   //no left by, should throw error
	   feedback = new Feedback(leftFor: userAccount2)
	   feedback.validate()
	   assert(feedback.hasErrors() == true)
	   
	   feedback = new Feedback(auction: auction, leftBy: userAccount1, leftFor: userAccount2, availability: 5, accuracy: 3, communication: 2, overallRating: 5, comment: "Blah blah, terrible item")
	   feedback.validate()
	   assert(feedback.hasErrors() == false)
	   
	   //out of range
	   feedback = new Feedback(auction: auction, leftBy: userAccount1, leftFor: userAccount2, availability: 6, accuracy: 3, communication: 2, overallRating: 5, comment: "Blah blah, terrible item")
	   feedback.validate()
	   assert(feedback.hasErrors() == true)
	   
	   //out of range
	   feedback = new Feedback(auction: auction, leftBy: userAccount1, leftFor: userAccount2, availability: 0, accuracy: 3, communication: 2, overallRating: 5, comment: "Blah blah, terrible item")
	   feedback.validate()
	   assert(feedback.hasErrors() == true)
	   
	   //no comment, not allowed
	   feedback = new Feedback(auction: auction, leftBy: userAccount1, leftFor: userAccount2, availability: 5, accuracy: 3, communication: 2, overallRating: 5, comment: " ")
	   feedback.validate()
	   assert(feedback.hasErrors() == true)
	   
	   //huge comment, not allowed
	   feedback = new Feedback(auction: auction, leftBy: userAccount1, leftFor: userAccount2, availability: 5, accuracy: 3, communication: 2, overallRating: 5, comment: "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
	   feedback.validate()
	   assert(feedback.hasErrors() == true)
    }
}
